package com.work.controller;

import com.google.gson.Gson;
import com.work.bean.Student;
import com.work.bean.Teacher;
import com.work.bean.User;
import com.work.dao.TeacherDao;
import com.work.dao.UserDao;
import com.work.dao.impl.TeacherDaoImpl;
import com.work.dao.impl.UserDaoImpl;
import com.work.service.TeacherService;
import com.work.service.impl.TeacherServiceImpl;
import com.work.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class TeacherServlet extends BaseServlet{
    TeacherService teacherService = new TeacherServiceImpl();
    UserDao userDao = new UserDaoImpl();
    TeacherDao teacherDao = new TeacherDaoImpl();
    protected void queryTeacher(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        List<Teacher> teachers = teacherService.queryTeacher();
        req.setAttribute("teachers",teachers);
         req.getRequestDispatcher("/pages/admin/teacher/admin_teacher.jsp").forward(req, resp);
    }
    protected void queryTeacher1(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        List<Teacher> teachers = teacherService.queryTeacher();
        Gson gson=new Gson();
        String json=gson.toJson(teachers);
        System.out.println(json);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.println(json);
    }
    protected void updateTeacher(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
       Teacher teacher = WebUtils.copyParamToBean(req.getParameterMap(),new Teacher());
        if(((User)req.getSession().getAttribute("user")).getType().equals("0")){
       teacherService.updateTeacher(teacher);
       resp.sendRedirect(req.getContextPath()+"/TeacherServlet?action=queryTeacher");
        }else {
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            teacher.setName(name);
            teacher.setSex(sex);
            Teacher teacher1 = ((Teacher)req.getSession().getAttribute("teacher"));
            teacher.setId(teacher1.getId());
            int uid = WebUtils.parseInt(((User)req.getSession().getAttribute("user")).getId()+"",0);
            teacher.setuId(uid);
            teacher.setPwd(teacher1.getPwd());
            teacherService.updateTeacher(teacher);
            req.getSession().setAttribute("teacher",teacher);
            req.getRequestDispatcher("/pages/teacher/teacher.jsp").forward(req, resp);
        }
    }
    protected void deleteTeacher(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        teacherService.deleteTeacher(id);
        resp.sendRedirect(req.getContextPath() + "/TeacherServlet?action=queryTeacher");
    }
    protected void saveTeacher(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        Teacher teacher = WebUtils.copyParamToBean(req.getParameterMap(), new Teacher());
        if(((User)req.getSession().getAttribute("user")).getType().equals("0")) {
            teacherService.saveTeacher(teacher);
            resp.sendRedirect(req.getContextPath() + "/TeacherServlet?action=queryTeacher");
        }else{
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            teacher.setName(name);
            teacher.setSex(sex);
            Teacher teacher1 = ((Teacher)req.getSession().getAttribute("teacher"));
            User user = ((User)req.getSession().getAttribute("user"));
            int uid = WebUtils.parseInt(((User)req.getSession().getAttribute("user")).getId()+"",0);
            teacher.setuId(uid);
            teacher.setPwd(user.getUpass());
            teacherService.saveTeacher(teacher);
            teacher.setId(teacherDao.query().getId());
            req.getSession().setAttribute("teacher",teacher);
            req.getRequestDispatcher("/pages/teacher/teacher.jsp").forward(req, resp);
        }
    }
    protected void queryTeacherById(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        Teacher teacher = teacherService.queryTeacherById(id);
        req.setAttribute("teacher",teacher);
        req.getRequestDispatcher("/pages/admin/teacher/admin_teacher_edit.jsp").forward(req, resp);
    }
    protected void updatePassword(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        int id = WebUtils.parseInt(((User)req.getSession().getAttribute("user")).getId()+"",0);
        String password = req.getParameter("password");
        userDao.update(new User(id,null,password,null,null));
        User user = ((User)req.getSession().getAttribute("user"));
        user.setUpass(password);
        req.getSession().setAttribute("user",user);
        Teacher teacher = (Teacher)req.getSession().getAttribute("teacher");
        teacher.setPwd(password);
        teacherDao.updateTeacherPwdById(teacher);
        req.getRequestDispatcher("/pages/teacher/teacher.jsp").forward(req, resp);
    }
}
